package com.designmodel.proxymodel.dynamicproxy.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by
 * 生成cglib代理的类
 * @author touni
 * @date 2019/4/2
 * @time 11:11
 */
public class CglibDynamiProxy {


    public static CommitButton getCommitButtonProxy(){
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(CommitButton.class);
        //设置回调函数
        enhancer.setCallback(new MethodInterceptor() {
            //代理对象的方法执行，回调函数就会执行
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                //如果方法名符合click则执行
                if ("click".equalsIgnoreCase(method.getName())){
                    System.out.println("代理对象执行，当前使用的是click方法");
                }
                return methodProxy.invokeSuper(o,objects);
            }
        });




        return (CommitButton) enhancer.create();
    }



}
